/*
 * @Descripttion:
 * @version:
 * @Author: hutao
 * @Date: 2023-04-10 14:16:10
 * @LastEditors: hutao
 * @LastEditTime: 2023-08-28 15:50:14
 */
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import routes from './router'
import store from './store'
import ElementUI from 'element-ui'
import './styles.scss'
import Htui from 'htui-yllkbz'
import 'htui-yllkbz/lib/htui.css'
import './styles.scss'
import { _axios, baseConfig } from 'vue-kst-auth'

import HtTips from '@/components/htTips.vue'
import icon from '@/components/icon.vue'
// fade/zoom 等
import 'element-ui/lib/theme-chalk/base.css'
// collapse 展开折叠
import CollapseTransition from 'element-ui/lib/transitions/collapse-transition'
import VueQuillEditor from 'vue-quill-editor' //引入富文本编译器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

Vue.use(VueQuillEditor) //lib/transitions/collapse-transition
Vue.component(CollapseTransition.name, CollapseTransition)
// import axios from "axios";
/** 语言 */
//const languages = baseConfig.getCookie("languages") || "zh-Hans";

// import en from '../node_modules/element-ui/lib/locale/lang/en.js';
// import zhCN from '../node_modules/element-ui/lib/locale/lang/zh-CN.js';

import Print from './plugins/print.js'
Vue.use(Print) // 注册

Vue.use(ElementUI)
Vue.component('icon', icon)
Vue.use(Htui)
Vue.use(VueRouter)
Vue.component('tips', HtTips)

// const on = Vue.prototype.$on

// Vue.prototype.$on = function (event, func) {
//   let previous = 0
//   let newFunc = func
//   if (event === 'click') {
//     newFunc = function () {
//       const now = new Date().getTime()
//       if (previous + 1000 <= now) {
//         func.apply(this, arguments)
//         previous = now
//       }
//     }
//   }
//   on.call(this, event, newFunc)
// }

/** 设置axios返回类型 */
Vue.config.productionTip = false
/** 获取配置文件 */
_axios
  .get(`${process.env.BASE_URL.replace(/\/$/, '')}/baseConfig.json`)
  .then(async (res: any) => {
    // const lang = await fetch(`${process.env.BASE_URL.replace(/\/$/, "")}/locale/${languages}.json`)
    //   .then((res) => res.json()).catch(() => ({}));

    // (window as any)["i18n"] = Object.assign({}, (window as any).i18n, lang)

    /** 将配置文件转换赋值 */
    store.commit('setBaseData', res.data)

    const router = new VueRouter({
      mode: 'history',
      base: res.data.baseUrl,
      routes,
    })
    router.beforeEach((to: any, from: any, next: any) => {
      /* 路由发生变化修改页面meta */
      if (to.meta.content) {
        const head = document.getElementsByTagName('head')
        const meta = document.createElement('meta')
        meta.content = to.meta.content
        head[0].appendChild(meta)
      }
      /* 路由发生变化修改页面title */
      if (to.meta.title) {
        document.title = to.meta.title
      }
      if (baseConfig.getLoginState() || /login/.test(to.path)) {
        // 授权配置
        baseConfig.setOAuthConfig(store.getters.getBaseData('oAuthConfig'))
        baseConfig.setLoginTitle(store.getters.getBaseData('enterpriseName')) // 企业名称
        // 设置验证租户有效性地址
        baseConfig.setIsTenantAvailable(
          store.getters.getBaseData('isTenantAvailable')
        )
        // 设置登录地址
        baseConfig.setLoginUrl(store.getters.getBaseData('loginUrl'))
        // 设置获取当前登录租户相关信息地址
        baseConfig.setcurrentLoginInfoUrl(
          store.getters.getBaseData('currentLoginInfoUrl')
        )
        /** 获取内存权限信息 */
        const commonDatas: any = window.localStorage.getItem('commonDatas')
        let grantedPolicies: any = {}
        if (commonDatas && JSON.parse(commonDatas)) {
          grantedPolicies = JSON.parse(commonDatas).SessionState.auth
            .grantedPolicies
        }

        next()
      } else {
        next(false)
        next('/login')
      }
    })

    new Vue({
      router,
      store,
      render: (h) => h(App),
    }).$mount('#app')
  })
